package com.yr.ryjk.patient;

import java.text.ParseException;
import java.util.Map;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yr.ryjk.common.ErrorType;
import com.yr.ryjk.interceptor.authority.For;
import com.yr.ryjk.interceptor.authority.UserType;
import com.yr.ryjk.patient.entity.Patient;
import com.yr.ryjk.patient.service.IPatientService;
import com.yr.ryjk.util.JSONUtil;

/**
 * 就诊人
 * @author LiuZhang
 *
 */
@For(user=UserType.APP_USER)
@Controller
@RequestMapping("/patient")
public class PatientController {
	@Autowired
	private IPatientService patientService;

	/**
	 * app用户增加就诊人接口
	 * @param token
	 * @param name
	 * @param gender
	 * @param birthday
	 * @param phone
	 * @param certificateType
	 * @param certificateNo
	 * @param isSelf
	 * @return
	 * @throws ParseException 
	 */
	@For(user=UserType.APP_USER)
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public Object add(@RequestParam("token") @NotBlank String token,
			@RequestParam("name") @NotBlank String name,
			@RequestParam("gender")int gender,
			@RequestParam("birthday") @NotBlank String birthday,
			@RequestParam("phone") String phone,
			int certificateType,
			String certificateNo,
			@RequestParam(value="isSelf",defaultValue="1") int isSelf) throws ParseException{
		Patient p=patientService.add(token,name,gender,birthday,phone,certificateType,certificateNo,isSelf);
		return JSONUtil.getJSON(ErrorType.SUCCESS,p);
	} 
	
	/**
	 * app用户修改就诊人接口
	 * @param id
	 * @param name
	 * @param gender
	 * @param birthday
	 * @param phone
	 * @param certificateType
	 * @param certificateNo
	 * @param isSelf
	 * @return
	 * @throws ParseException 
	 */
	@For(user=UserType.APP_USER)
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> update(@RequestParam("token") @NotBlank String token,@RequestParam("id") @NotBlank String id,@RequestParam("name") @NotBlank String name,
			@RequestParam("gender") @NotBlank int gender,@RequestParam("birthday") @NotBlank String birthday,
			@RequestParam("phone") String phone,@RequestParam("certificateType") int certificateType,
			@RequestParam("certificateNo") String certificateNo,@RequestParam("isSelf") @NotBlank int isSelf
 			) throws ParseException{
		return patientService.update(token,id,name,gender,birthday,phone,certificateType,certificateNo,isSelf);
	} 
	
	/**
	 * 后台删除患者
	 * @param id 用户id
	 * @return
	 */
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delete(@RequestParam("id") @NotBlank String id){
		patientService.deleteById(id);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	
	/**
	 * 查询列表
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/listAll",method=RequestMethod.GET)
	@ResponseBody
	public Object list(@RequestParam("token") @NotBlank String token){
		return patientService.getListAll(token);
	}
}